<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增/编辑地址</title>
    <script type="text/javascript" src="/js/jquery.js"></script>
</head>
<body>
<form action="" id="J_form">
    <input type="hidden" id = "J_id" name="id" value ="${id}">
    <div>名字:<input type="text" id = "J_name" name="name" value =""></div>
    <div>电话号码:<input type="text" id = "J_phone" name="phone" value =""></div>
    <div>地址:<input type="text" id = "J_address" name="address" value =""></div>
    <input type="checkbox" id="J_detault" name ="isdefault" value="1"> 是否默认
    <button type = "button" id="J_submit"> 提交</button>
</form>

<script>
    $(function(){
        var id = "${id}";
        if(id != 0){
           getAddressDetail(id);
        }
        $("#J_submit").click(function(){
            var data = {id:$("#J_id").val(),uid:"${uid}",name:$("#J_name").val(),phone:$("#J_phone").val(),address:$("#J_address").val(),isdefault:$("#J_detault").val()};
            var url = "/address/edit_address";
            $.ajax({
                type: "POST",
                dataType: 'json',
                async: false,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                url: url,
                success: function(res){
                    if(res.code == "200"){
                       alert("保存成功");
                       window.location.href="/userPage/address_list";
                    }else{
                        alert(res.msg);
                    }
                },
                error: function(){

                },
                complete: function(){
                }
            });

        });
    })

    function getAddressDetail(id){
        var data = {id:id};
        var url = "/address/address_detail";
        $.ajax({
            type: "POST",
            dataType: 'json',
            async: false,
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data),
            url: url,
            success: function(res){
                if(res.code == "200"){
                    var list= res.data;
                    $("#J_name").val(list['name']);
                    $("#J_phone").val(list['phone']);
                    $("#J_address").val(list['address']);
                }else{
                    alert(res.msg);
                }
            },
            error: function(){

            },
            complete: function(){
            }
        });
    }

</script>
</body>
</html>
